package com.ize.service.impl;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ize.entity.dto.UpdateArticleDto;
import com.ize.entity.dto.addArticleDto;
import com.ize.entity.pojo.Article;
import com.ize.entity.pojo.PageBean;
import com.ize.entity.pojo.User;
import com.ize.mapper.ArticleMapper;
import com.ize.service.ArticleService;
import com.ize.util.AliyunOssUtil;
import com.ize.util.BaseContext;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    private final ArticleMapper articleMapper;

    @Override
    public PageBean<Article> SelectArticle(Integer pageNum, Integer pageSize, String state ,String key) {
        //1.创建pageBean对象
        PageBean<Article> pageBean = new PageBean<>();

        //2.开启分页查询 pagehelper
        PageHelper.startPage(pageNum,pageSize);

        Long currentId = BaseContext.getCurrentId();
        System.out.println(currentId);
        //3.调用mapper
        List<Article> as = articleMapper.SelectArticle(currentId,state,key);

        //page提供了方法，可以获取pageHelper分页查询后，得到的总记录条数和当前页数据
        Page<Article> p = (Page<Article>) as;

        //填充数据
        pageBean.setTotal(p.getTotal());
        pageBean.setItems(p.getResult());

        return pageBean;

    }

    /**
     * 新增文章
     * @param addArticleDto
     */
    @Override
    public void insert(addArticleDto addArticleDto) {
        Article article = new Article();
        BeanUtils.copyProperties(addArticleDto,article);
        article.setDoctorAdminId(BaseContext.getCurrentId());
        article.setImage(addArticleDto.getCoverImg());
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.insert(article);
    }

    /**
     * 修改
     * @param updateArticleDto
     */
    @Override
    public void update(UpdateArticleDto updateArticleDto) {
        Article article = new Article();
        BeanUtils.copyProperties(updateArticleDto,article);
        article.setImage(updateArticleDto.getCoverImg());
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.updateById(article);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        Article article = articleMapper.selectById(id);
        String image = article.getImage();
        String s = extractFileName(image);
        try {
            AliyunOssUtil.deletePic(s);
        }catch (Exception e){
            log.error("云服务图片没有删除");
            e.printStackTrace();
        }
        articleMapper.deleteById(id);
    }

    @Override
    public PageBean<Article> select(Integer page, Integer pageSize) {
        //1.创建pageBean对象
        PageBean<Article> pageBean = new PageBean<>();

        //2.开启分页查询 pagehelper
        PageHelper.startPage(page,pageSize);

        Long currentId = BaseContext.getCurrentId();
        System.out.println(currentId);
        //3.调用mapper
        QueryWrapper<Article> wrapper = Wrappers.query();
        List<Article> as = articleMapper.selectList(wrapper);

        //page提供了方法，可以获取pageHelper分页查询后，得到的总记录条数和当前页数据
        Page<Article> p = (Page<Article>) as;

        //填充数据
        pageBean.setTotal(p.getTotal());
        pageBean.setItems(p.getResult());

        return pageBean;
    }

    @Override
    public Article getDtail(Integer id) {
        return articleMapper.selectById(id);
    }


    // 方法：从 URL 中提取文件名
    public static String extractFileName(String url) {
        // 通过"/"分割 URL，并取最后一个部分，即文件名
        String[] parts = url.split("/");
        return parts[parts.length - 1]; // 返回最后一个元素
    }

}
